package cn.huhst.cas.weisiqian.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import cn.huhst.cas.weisiqian.dbhelp.DBHelper;
import cn.huhst.cas.weisiqian.pojo.COMMENT_TYPE;
import cn.huhst.cas.weisiqian.pojo.Comment;
import cn.huhst.cas.weisiqian.pojo.News;

public class NewsDao {

	private Logger logger = Logger.getLogger(NewsDao.class);

	public boolean addPostType(News news) {
		Connection connection = DBHelper.getConnection();
		PreparedStatement pstmt = null;
		String sql = "INSERT INTO news(title,content,author,writetime) value(?,?,?,?)";
		logger.info("sql:" + sql);
		boolean result = false;
		try {
			pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, news.getTitle());
			pstmt.setString(2, news.getContent());
			pstmt.setString(3, news.getAuthor());
			pstmt.setDate(4, news.getWritetime());
			int count = pstmt.executeUpdate();
			if (count > 0) {
				logger.info("添加新闻成功");
				logger.debug("添加新闻成功：" + news);
				result = true;
			}
		} catch (SQLException e) {
			logger.error("添加新闻失败", e);
		} finally {
			DBHelper.closeDBHelp(connection, pstmt, null);
			logger.info("关闭数据库连接");
		}

		return result;
	}

	public int getNewsCount() {
		int count = 0;
		Connection connection = DBHelper.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "SELECT COUNT(*) FROM news";
		logger.info("sql:" + sql);
		try {
			pstmt = connection.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				count = rs.getInt(1);
				logger.info("查询新闻数目成功,count=" + count);
			}
		} catch (SQLException e) {
			logger.error("查询新闻数目出错", e);
		} finally {
			DBHelper.closeDBHelp(connection, pstmt, null);
			logger.info("关闭数据库连接");
		}
		return count;
	}

	public ArrayList<News> getNews(int recorderIndex, int pageSize) {
		logger.info("recorderIndex=" + recorderIndex + " pageSize=" + pageSize);
		ArrayList<News> allNews = new ArrayList<News>();
		Connection connection = DBHelper.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "SELECT * FROM news LIMIT ?,?";
		logger.info("sql:" + sql);
		try {
			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, recorderIndex);
			pstmt.setInt(2, pageSize);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				News news = new News();
				news.setNewsid(rs.getInt("newsid"));
				news.setTitle(rs.getString("title"));
				news.setContent(rs.getString("content"));
				news.setAuthor(rs.getString("author"));
				news.setWritetime(rs.getDate("writetime"));
				news.setViewcount(rs.getInt("viewcount"));
				logger.debug(news);
				allNews.add(news);
			}
		} catch (SQLException e) {
			logger.error("分页查询新闻出错", e);
		} finally {
			DBHelper.closeDBHelp(connection, pstmt, rs);
			logger.info("关闭数据库连接");
		}
		return allNews;
	}

	public boolean deleteNews(String newsid) {
		Connection connection = DBHelper.getConnection();
		PreparedStatement pstmt = null;
		String sql = "DELETE FROM news WHERE newsid=?";
		logger.info("sql:" + sql);
		int count = 0;
		try {
			pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, newsid);
			count = pstmt.executeUpdate();
			logger.info("count=" + count);
		} catch (SQLException e) {
			logger.error("删除新闻：" + newsid + " 失败");
		} finally {
			DBHelper.closeDBHelp(connection, pstmt, null);
			logger.info("关闭数据库连接");
		}
		return count > 0 ? true : false;
	}

	public News getNewsByNewsID(String newsid) {
		News news = new News();
		Connection connection = DBHelper.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String viewsql = "SELECT * FROM news WHERE newsid=?";
		String addviewcountsql = "UPDATE news SET viewcount=? WHERE newsid=?";
		logger.info("sql:" + viewsql);
		// 获取新闻内容
		try {
			pstmt = connection.prepareStatement(viewsql);
			pstmt.setString(1, newsid);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				news.setNewsid(Integer.parseInt(newsid));
				news.setAuthor(rs.getString("author"));
				news.setTitle(rs.getString("title"));
				news.setContent(rs.getString("content"));
				news.setWritetime(rs.getDate("writetime"));
				news.setViewcount(rs.getInt("viewcount") + 1);
				logger.debug("查看新闻：" + news);
			}
		} catch (SQLException e) {
			logger.error("查询newid为" + newsid + "新闻失败");
		}
		// 更新新闻浏览次数
		try {
			pstmt = connection.prepareStatement(addviewcountsql);
			pstmt.setInt(1, news.getViewcount());
			pstmt.setString(2, newsid);
			int count = pstmt.executeUpdate();
			if (count > 0) {
				logger.info("newsid=" + news.getNewsid() + " 标题：" + news.getTitle() + " 新闻浏览量加一");
			} else {
				logger.error("更新newid为" + newsid + "新闻浏览次数失败");
			}
		} catch (SQLException e) {
			logger.error("更新newid为" + newsid + "新闻浏览次数失败");
		} finally {
			DBHelper.closeDBHelp(connection, pstmt, rs);
			logger.info("关闭数据库连接");
		}
		return news;
	}

	public ArrayList<Comment> getComment(int newsid, COMMENT_TYPE newsComment) {
		logger.info("newsid=" + newsid + " newsComment=" + newsComment);
		ArrayList<Comment> comments = new ArrayList<Comment>();
		Connection connection = DBHelper.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "SELECT * FROM comment WHERE parentid=? AND commenttype=?";
		logger.info("sql:" + sql);
		try {
			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, newsid);
			pstmt.setInt(2, newsComment.getCommenttype());
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Comment comment = new Comment();
				comment.setAuthor(rs.getString("author"));
				comment.setCommentid(rs.getInt("commentid"));
				comment.setCommenttype(rs.getInt("commenttype"));
				comment.setContent(rs.getString("content"));
				comment.setDislikecount(rs.getInt("dislikecount"));
				comment.setLikecount(rs.getInt("likecount"));
				comment.setParentid(rs.getInt("parentid"));
				comment.setWritetime(rs.getDate("writetime"));
				comments.add(comment);
				logger.debug(comment);
			}
		} catch (SQLException e) {
			logger.error("查询新闻（newsid=" + newsid + "）评论失败");
		} finally {
			DBHelper.closeDBHelp(connection, pstmt, rs);
			logger.info("关闭数据库连接");
		}
		return comments;
	}

}
